package com.lineage.server.utils;

import java.math.BigDecimal;

public class DoubleUtil {
  public static double round(double value, int scale, int roundingMode) {
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(scale, roundingMode);
    double d = bd.doubleValue();
    bd = null;
    return d;
  }
  
  public static double sum(double d1, double d2) {
    BigDecimal bd1 = new BigDecimal(Double.toString(d1));
    BigDecimal bd2 = new BigDecimal(Double.toString(d2));
    return bd1.add(bd2).doubleValue();
  }
  
  public static double sub(double d1, double d2) {
    BigDecimal bd1 = new BigDecimal(Double.toString(d1));
    BigDecimal bd2 = new BigDecimal(Double.toString(d2));
    return bd1.subtract(bd2).doubleValue();
  }
  
  public static double mul(double d1, double d2) {
    BigDecimal bd1 = new BigDecimal(Double.toString(d1));
    BigDecimal bd2 = new BigDecimal(Double.toString(d2));
    return bd1.multiply(bd2).doubleValue();
  }
  
  public static double div(double d1, double d2, int scale) {
    BigDecimal bd1 = new BigDecimal(Double.toString(d1));
    BigDecimal bd2 = new BigDecimal(Double.toString(d2));
    return bd1.divide(bd2, scale, 4).doubleValue();
  }
}
